package blog.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

import blog.ConstString;
import blog.SysRuntime;
import blog.controller.*;
import blog.db.BlogAccount;
import blog.db.BlogAccountDB;
import blog.util.StringUtils;

public class BlogAccountGui extends Form implements IBaseGui, CommandListener, ItemStateListener {
	
	public static final int OP_TYPE_ADD = 1;
	public static final int OP_TYPE_EDIT = 2;
	
	private static Command cmdConfirm = new Command(ConstString.CMD_ACTIVATE, Command.OK, 0);
	private static Command cmdBack = new Command(ConstString.CMD_BACK, Command.BACK, 0);
	
	private static TextField accountName;
	private static TextField blogUrl;
	private static TextField userName;
	private static TextField password;
	private static ChoiceGroup blogType;
	private static TextField fullUrl;
	//private static TextField blogId;
	
	static
	{
		accountName = new TextField(ConstString.GUI_BLOG_ACCOUNT_ACCOUNT_NAME, "", 50, TextField.ANY);
		blogUrl = new TextField(ConstString.GUI_BLOG_ACCOUNT_BLOG_URL, "http://", 200, TextField.URL);
		userName = new TextField(ConstString.GUI_BLOG_ACCOUNT_USER_NAME, "", 50, TextField.ANY);
		password = new TextField(ConstString.GUI_BLOG_ACCOUNT_PASSWORD, "", 50, TextField.PASSWORD);
		blogType = new ChoiceGroup(ConstString.GUI_BLOG_ACCOUNT_BLOG_TYPE, ChoiceGroup.POPUP);
		blogType.append(ConstString.GUI_BLOG_ACCOUNT_BLOG_TYPE_MOVABLE, null);
		blogType.append(ConstString.GUI_BLOG_ACCOUNT_BLOG_TYPE_METAWEBLOG, null);
		blogType.append(ConstString.GUI_BLOG_ACCOUNT_BLOG_TYPE_BLOGGER, null);
		blogType.append(ConstString.GUI_BLOG_ACCOUNT_BLOG_TYPE_WORDPRESS, null);
		blogType.setFitPolicy(ChoiceGroup.TEXT_WRAP_ON);
		fullUrl = new TextField(ConstString.GUI_BLOG_ACCOUNT_FULL_URL, "http://", 200, TextField.URL);
		//blogId = new TextField(ConstString.GUI_BLOG_ACCOUNT_BLOG_ID, "", 200, TextField.ANY);
	}
	
	private int operationType;
	private BlogAccount account;
	
	private static BlogAccountGui _instance;
	
	public synchronized static BlogAccountGui getInstance()
	{
		if ( _instance==null )
		{
			_instance = new BlogAccountGui();
		}
		return _instance;
	}
	
	private BlogAccountGui()
	{
		super(ConstString.GUI_TITLE_BLOG_ACCOUNT);
		this.addCommand(cmdBack);
		this.addCommand(cmdConfirm);
		this.setCommandListener(this);
		
		this.append(accountName);
		this.append(blogUrl);
		this.append(userName);
		this.append(password);
		this.append(blogType);
		this.append(fullUrl);
		//this.append(blogId);
		
		this.setItemStateListener(this);
	}
	
	public void setOperationType( int operationType )
	{
		this.operationType = operationType;
	}
	
	public void setData( BlogAccount account )
	{
		if ( account!=null )
		{
			this.account = account;
			accountName.setString(account.getAccountName());
			userName.setString(account.getUserName());
			password.setString(account.getPassword());
			blogType.setSelectedIndex(account.getBlogType()-1, true);
			accountName.setString(account.getAccountName());
			fullUrl.setString(account.getFullUrl());
			//blogId.setString(account.getBlogId());
			blogUrl.setString(account.getBlogUrl());
		}
		else
		{
			setData( new BlogAccount() );
		}
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_BLOG_ACCOUNT;
	}

	public void notifyChanged(Object param) {

	}

	public void commandAction(Command arg0, Displayable arg1) {
		if ( arg0==cmdBack )
		{
			BlogAccountManagerGui.getInstance( ).reloadData( );
			SysRuntime.guiManager.back();
		}
		else if ( arg0==cmdConfirm )
		{
			if ( !checkOk() )
			{
				return;
			}
			account.setAccountName(accountName.getString());
			account.setUserName(userName.getString());
			account.setPassword(password.getString());
			account.setBlogType(blogType.getSelectedIndex()+1);
			account.setFullUrl(fullUrl.getString());
			//account.setBlogId(blogId.getString());
			account.setBlogUrl(blogUrl.getString());
			account.setChecked( false );
			if ( this.operationType==OP_TYPE_ADD )
			{
				BlogAccountDB.add(account);
				SysRuntime.actionManager.addAction(this, ConstAction.ACT_GET_BLOG_WHEN_ADD, account);
			}
			else if ( this.operationType==OP_TYPE_EDIT )
			{
				BlogAccountDB.update(account);
				SysRuntime.actionManager.addAction(this, ConstAction.ACT_GET_BLOG_WHEN_EDIT, account);
			}
			//BlogAccountManagerGui.getInstance().reloadData();
			//SysRuntime.guiManager.back();
		}
	}
	
	private boolean checkOk()
	{
		String _accountName = accountName.getString();
		if ( _accountName==null || _accountName.length()==0 )
		{
			Alert prompt = new Alert(ConstString.GUI_TITLE_PROMPT, StringUtils.combin(ConstString.PROMPT_NO_EMPTY, ConstString.GUI_BLOG_ACCOUNT_ACCOUNT_NAME), null, AlertType.WARNING);
			prompt.setTimeout(5000);
			SysRuntime.display.setCurrent(prompt, this);
			return false;
		}
		
		String _userName = userName.getString();
		if ( _userName==null || _userName.length()==0 )
		{
			Alert prompt = new Alert(ConstString.GUI_TITLE_PROMPT, StringUtils.combin(ConstString.PROMPT_NO_EMPTY, ConstString.GUI_BLOG_ACCOUNT_USER_NAME), null, AlertType.WARNING);
			prompt.setTimeout(5000);
			SysRuntime.display.setCurrent(prompt, this);
			return false;
		}
		
		String _blogUrl = blogUrl.getString();
		if ( _blogUrl==null || _blogUrl.length()==0 )
		{
			Alert prompt = new Alert(ConstString.GUI_TITLE_PROMPT, StringUtils.combin(ConstString.PROMPT_NO_EMPTY, ConstString.GUI_BLOG_ACCOUNT_BLOG_URL), null, AlertType.WARNING);
			prompt.setTimeout(5000);
			SysRuntime.display.setCurrent(prompt, this);
			return false;
		}
		else
		{
			if ( !_blogUrl.startsWith("http://") || _blogUrl.length()<=7 )
			{
				Alert prompt = new Alert(ConstString.GUI_TITLE_PROMPT, StringUtils.combin(ConstString.PROMPT_INVALID_URL, ConstString.GUI_BLOG_ACCOUNT_BLOG_URL), null, AlertType.WARNING);
				prompt.setTimeout(5000);
				SysRuntime.display.setCurrent(prompt, this);
				return false;
			}
		}

		String _fullUrl = fullUrl.getString();
		if ( _fullUrl==null || _fullUrl.length()==0 )
		{
			Alert prompt = new Alert(ConstString.GUI_TITLE_PROMPT, StringUtils.combin(ConstString.PROMPT_NO_EMPTY, ConstString.GUI_BLOG_ACCOUNT_FULL_URL), null, AlertType.WARNING);
			prompt.setTimeout(5000);
			SysRuntime.display.setCurrent(prompt, this);
			return false;
		}
		else
		{
			if ( !_fullUrl.startsWith("http://") || _fullUrl.length()<=7 )
			{
				Alert prompt = new Alert(ConstString.GUI_TITLE_PROMPT, StringUtils.combin(ConstString.PROMPT_INVALID_URL, ConstString.GUI_BLOG_ACCOUNT_FULL_URL), null, AlertType.WARNING);
				prompt.setTimeout(5000);
				SysRuntime.display.setCurrent(prompt, this);
				return false;
			}
		}
		
		return true;
	}

	public void itemStateChanged(Item item) {
		if ( item==blogType || item==userName )
		{
			int _blogType = blogType.getSelectedIndex()+1;
			String _userName = userName.getString();
			if ( _blogType==BlogAccount.TYPE_MOVABLE )
			{
				
			}
			else if ( _blogType==BlogAccount.TYPE_METAWEBLOG )
			{
				
			}
			else if ( _blogType==BlogAccount.TYPE_BLOGGER )
			{
				
			}
			else if ( _blogType==BlogAccount.TYPE_WORDPRESS )
			{
				
			}
		}
	}
}